package com.yulan.Heap;

import org.junit.Test;

import java.util.*;

public class LeetCode_347 {


    public int[] topKFrequent(int[] nums, int k) {
        if(nums == null || nums.length == 0) return new int[]{};
        Map<Integer,Integer> map = new HashMap<>();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>(( o1, o2) -> {
            return map.get(o2) - map.get(o1);
        });
        int[] ans = new int[k];
        for(int num : nums){
            map.put(num,map.getOrDefault(num,0)+1);
        }

        for(int key:map.keySet()){
            priorityQueue.offer(key);
        }

        for(int i = 0 ;i<k;i++){
            ans[i] = priorityQueue.poll();
        }

        return ans;
    }


    @Test
    public void test(){
        int[] arr = {4,1,-1,2,-1,2,3};
        for(int num : topKFrequent(arr,2))
            System.out.print(num+" ");
    }
}
